<?php
/*
 * CRMHtml File
 *
 * @link http://riedel-st.de
 * @author Stefan Riedel <sr@riedel-st.de>
*/

/**
 * Description of CRMHtml
 *
 * @author Stefan Riedel
 */
class CRMHtml extends CHtml {
	
	public static function imageUrl($url) {
		return self::baseUrl () . '/images/' . $url;
	}
	
	public static function cssUrl($url) {
		return self::baseUrl () . '/css/' . $url;
	}
	
	public static function jsUrl($url) {
		return self::baseUrl () . '/js/' . $url;
	}
	
	public static function baseUrl() {
		return Yii::app ()->baseUrl;
	}
	
	/**
	 *
	 * the API is from CHtml::errorSummary()
	 *
	 * @param CActiveRecord $model
	 * @param string $header
	 * @param string $footer
	 * @param array $htmlOptions
	 * @return string
	 * @see CHtml::errorSummary()
	 */
	public static function errorSummary($model, $header = null, $footer = null, $htmlOptions = array()) {
		$content = '';
		if (! is_array ( $model )) {
			$model = array ($model );
		}
		foreach ( $model as $m ) {
			foreach ( $m->getErrors () as $errors ) {
				foreach ( $errors as $error ) {
					if ($error != '') {
						$content .= "<li>$error</li>\n";
					}
				}
			}
		}
		if ($content !== '') {
			if ($header === null) {
				$header = '<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>' . Yii::t ( 'crm', 'Es sind Fehler aufgetreten, diese müssen zuerst behoben werden:' ) . '</p>';
			}
			if (! isset ( $htmlOptions ['class'] )) {
				$htmlOptions ['class'] = "ui-state-highlight ui-corner-all errorSummary";
			}
			return CHtml::tag ( 'div', $htmlOptions, $header . "\n<ul>\n$content</ul>" . $footer );
		} else
			return '';
	}
	
	public static function ajaxButtonInTab($label, array $url, $name, $update = '', $method = 'POST', array $events = array()) {
		$method = strtoupper ( ( string ) $method );
		if ($events !== array ()) {
			$beforeEvents = array ('beforeSend' => '', 'complete' => '' );
			$afterEvents = array ('beforeSend' => '', 'complete' => '' );
			foreach ( $events as $ev => $evValue ) {
				if (isset ( $evValue ['before'] ) && ! empty ( $evValue ['before'] )) {
					$beforeEvents [$ev] = $evValue ['before'];
				}
				if (isset ( $evValue ['after'] ) && ! empty ( $evValue ['after'] )) {
					$afterEvents [$ev] = $evValue ['after'];
				}
			}
		}
		
		return CHtml::ajaxSubmitButton ( $label, $url, array ('type' => $method, 'update' => $update, 'beforeSend' => 'function() {
                ' . $beforeEvents ['beforeSend'] . '
                $(".ui-tabs-selected a:first-child").addClass("preloader");
                ' . $afterEvents ['beforeSend'] . '
            }', 'complete' => 'function() {
                ' . $beforeEvents ['complete'] . '
                $(".ui-tabs-selected a:first-child").removeClass("preloader");
                ' . $afterEvents ['complete'] . '
            }' ), array ('name' => $name, 'class' => 'ui-button ui-state-default ui-corner-all' ) );
	}
	
	public static function normalizeUrlWithCheckAccess(array $url, $access, array $accessParams = array()) {
		$link = '';
		if (Yii::app ()->user->checkAccess ( $access, $accessParams )) {
			$link = CHtml::normalizeUrl($url);
		}	
		return $link;
	}
	
	public static function linkWithCheckAccess($access, $text, $accessParams = array(), $url = '#', $htmlOptions = array()) {
		$link = '';
		if (Yii::app ()->user->checkAccess ( $access, $accessParams )) {
			$link = CHtml::link ( $text, $url, $htmlOptions );
		}
		return $link;
	}
	
	public static function numberedLinkToCall($number, $amt = null, $areaCode = null, $countryCode = null) {
		$numberText = (! empty ( $areaCode )) ? $areaCode . '-' : '';
		$numberText .= $number;
		return self::linkToCall($numberText, $number, $amt, $areaCode, $countryCode);
	}
	
	public static function linkToCall($text, $number, $amt = null, $areaCode = null, $countryCode = null) {
		$number = $amt . $countryCode . $areaCode . $number;
		$url = array ('/asterisk/telephone/call', 'number' => $number );
		return self::linkWithCheckAccess ( 'AsteriskTelephoneCall', $text, array ('number' => $number ), '#', array ('class' => 'icon telephone', 'onclick' => CHtml::ajax ( array ('url' => $url ) ) ) );
	}
}
?>
